﻿#include "DoubleSpinBoxPropertyEditEntry.h"
#include <QDoubleSpinBox>

#include "UiTextEditEntry.h"
#include "IntegerEditEntry.h"
#include "DoubleEditEntry.h"
#include "EnumEditEntry.h"

#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(DoubleSpinBox)

struct DoubleSpinBoxPropertyEditEntry::DoubleSpinBoxPropertyEditEntryPrivate {
    QPointer<QDoubleSpinBox> object;

    QPointer<UiTextEditEntry> entrySuffix;
    QPointer<UiTextEditEntry> entryPrefix;
    QPointer<DoubleEditEntry> entryMinimum;
    QPointer<DoubleEditEntry> entryMaximum;
    QPointer<DoubleEditEntry> entrySingleStep;
    QPointer<EnumEditEntry> entryStepType;
    QPointer<DoubleEditEntry> entryValue;
};

DoubleSpinBoxPropertyEditEntry::~DoubleSpinBoxPropertyEditEntry() { }

DoubleSpinBoxPropertyEditEntry::DoubleSpinBoxPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new DoubleSpinBoxPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

void DoubleSpinBoxPropertyEditEntry::setObject(void *object) {
    p->object = static_cast<QDoubleSpinBox*>(object);
}

void DoubleSpinBoxPropertyEditEntry::onTick() {
    if(!p->object || !isVisible()) {
        return ;
    }

    p->entrySuffix->setValue(p->object->suffix());
    p->entryPrefix->setValue(p->object->prefix());
    p->entryMinimum->setValue(p->object->minimum());
    p->entryMaximum->setValue(p->object->maximum());
    p->entrySingleStep->setValue(p->object->singleStep());
    p->entryStepType->setValue(p->object->stepType());
    p->entryValue->setValue(p->object->value());
}

void DoubleSpinBoxPropertyEditEntry::initUi() {
    p->entrySuffix = new UiTextEditEntry;
    appendChildEntry(p->entrySuffix);
    connect(p->entrySuffix, &UiTextEditEntry::valueChanged, [=](const QString &value) {
        p->object->setSuffix(value);
    });

    p->entryPrefix = new UiTextEditEntry;
    appendChildEntry(p->entryPrefix);
    connect(p->entryPrefix, &UiTextEditEntry::valueChanged, [=](const QString &value) {
        p->object->setPrefix(value);
    });

    p->entryMinimum = new DoubleEditEntry;
    appendChildEntry(p->entryMinimum);
    connect(p->entryMinimum, &DoubleEditEntry::valueChanged, [=](double value) {
        p->object->setMinimum(value);
    });

    p->entryMaximum = new DoubleEditEntry;
    appendChildEntry(p->entryMaximum);
    connect(p->entryMaximum, &DoubleEditEntry::valueChanged, [=](double value) {
        p->object->setMaximum(value);
    });

    p->entrySingleStep = new DoubleEditEntry;
    appendChildEntry(p->entrySingleStep);
    connect(p->entrySingleStep, &DoubleEditEntry::valueChanged, [=](double value) {
        p->object->setSingleStep(value);
    });

    p->entryStepType = new EnumEditEntry;
    p->entryStepType->setMetaEnum(QMetaEnum::fromType<QDoubleSpinBox::StepType>());
    appendChildEntry(p->entryStepType);
    connect(p->entryStepType, &EnumEditEntry::valueChanged, [=](int value) {
        p->object->setStepType(QDoubleSpinBox::StepType(value));
    });

    p->entryValue = new DoubleEditEntry;
    appendChildEntry(p->entryValue);
    connect(p->entryValue, &DoubleEditEntry::valueChanged, [=](double value) {
        p->object->setValue(value);
    });
}

void DoubleSpinBoxPropertyEditEntry::retranslateUi() {
    setTitle(tr("QDoubleSpinBox"));
    p->entrySuffix->setTitle(tr("suffix"));
    p->entryPrefix->setTitle(tr("prefix"));
    p->entryMinimum->setTitle(tr("minimum"));
    p->entryMaximum->setTitle(tr("maximum"));
    p->entrySingleStep->setTitle(tr("singleStep"));
    p->entryStepType->setTitle(tr("stepType"));
    p->entryValue->setTitle(tr("value"));
}
